package com.clp.protocol.iec104.apdu.asdu.info_obj.info_elem;

import com.clp.protocol.iec104.apdu.asdu.info_obj.info_elem.file_transfer.AttachPack;
import com.clp.protocol.iec104.definition.Ft;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 文件传输 信息元素内容
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class F_FR_NA_2_InfoElem extends InfoElem {

    /**
     * 附加数据包类型
     */
    private Ft.AttachPackType attachPackType;

    /**
     * 附加数据包
     */
    private AttachPack attachPack;

    @Override
    public F_FR_NA_2_InfoElem initBy(ByteBuf buf) {
        this.attachPackType = Ft.AttachPackType.gain(buf.readByte());
        this.attachPack = Ft.OperationTag.gain(buf.getByte(buf.readerIndex()) & 0xFF).newInvalidAttachPack();
        return this;
    }

    @Override
    public boolean isValid() {
        if (attachPackType == null) return false;
        return attachPack != null && attachPack.isValid();
    }

    @Override
    public void writeTo(ByteBuf buf) {
        buf.writeByte(attachPackType.getVal());
        attachPack.writeTo(buf);
    }

    @Override
    public String toString() {
        return "附加数据包类型：" + attachPackType + ", 附加数据包：" + attachPack;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        F_FR_NA_2_InfoElem that = (F_FR_NA_2_InfoElem) o;
        return attachPackType == that.attachPackType && Objects.equals(attachPack, that.attachPack);
    }

    @Override
    public int hashCode() {
        return Objects.hash(attachPackType, attachPack);
    }
}
